package lsf.itcast.dao;

import lsf.itcast.domain.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {
    //一个用户可以多个角色，一个角色可以赋予多个用户
    //根据用户名查询用户和角色信息
    @Select("select * from users where userName=#{userName}")
    @Results({@Result(id = true,property = "id",column = "id"),
            @Result(id = true,property = "username",column = "username"),
            @Result(id = true,property = "email",column = "email"),
            @Result(id = true,property = "password",column = "password"),
            @Result(id = true,property = "phoneNum",column = "phoneNum"),
            @Result(id = true,property = "statusStr",column = "statusStr"),
            @Result(id = true,property = "roles",column = "id",javaType = java.util.List.class,
                    many = @Many(select = "lsf.itcast.dao.RoleDao.findRoleById"))

    })
    UserInfo findByUserId(String userName);


    //用户管理，查询所有用户
    @Select("select * from users")
    List<UserInfo> findAll();


    //查询用户，包含了权限和角色信息；
    //1.通过用户查询得到角色信息
    //2.在通过角色查询得到权限信息id
   @Select("select * from users where id=#{id}")
   @Results({@Result(id = true,property = "id",column = "id"),
           @Result(property = "username",column = "username"),
           @Result(property = "email",column = "email"),
           @Result(property = "password",column = "password"),
           @Result(property = "phoneNum",column = "phoneNum"),
           @Result(property = "statusStr",column = "statusStr"),
           @Result(property = "roles",column = "id",javaType = java.util.List.class,
                   many = @Many(select = "lsf.itcast.dao.RoleDao.findRoleById"))

   })
    UserInfo findById(String id);

   @Insert("insert into users(email,username,password,phoneNum,status) values(#{email},#{username},#{password},#{phoneNum},#{status})")
    void save(UserInfo userInfo);

    //根据users_role查询到userId,排除没有对应roleId的，然后给userId匹配角色，即roleId
    // 相当于给用户添加角色,即把角色ids添加到users_role中
    @Insert("insert into users_role(userId,roleId) values(#{userId},#{roleId})")
    void addRoleToUser(@Param("userId") String userId, @Param("roleId")String roleId);
}
